package com.credithc.customers.dataservice.service.impl;

import com.credithc.customers.base.constants.CustomerConstants;
import com.credithc.customers.base.constants.ProductTypeConstants;
import com.credithc.customers.base.utils.CustomerUtils;
import com.credithc.customers.dataservice.constants.SyncTypeEnum;
import com.credithc.customers.dataservice.constants.TableNameConstants;
import com.credithc.customers.dataservice.ro.BinlogColumnRo;
import com.credithc.customers.dataservice.ro.BinlogRo;
import com.credithc.customers.dataservice.ro.BinlogRowsRo;
import com.credithc.customers.dataservice.ro.ProductInfoRo;
import com.credithc.customers.dataservice.service.ProductInfoService;
import com.credithc.customers.dataservice.service.SyncDataService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;
@Service
@Slf4j
public class ProductInfoServiceImpl implements ProductInfoService {

    @Autowired
    private SyncDataService syncDataService;

    @Override
    public ProductInfoRo bulidDlrProduct(BinlogRo binlogRo) {
        BinlogRowsRo rowsRo = binlogRo.getRows();
        ProductInfoRo productInfoRo = new ProductInfoRo();
        String eventType = binlogRo.getEventType();
        productInfoRo.setSyncType(eventType);
        productInfoRo.setDatabaseName(binlogRo.getSchemaName());
        productInfoRo.setTableName(binlogRo.getTableName());
        if (SyncTypeEnum.DELETE.getValue().equals(eventType)){
            initDlrProduct(rowsRo.getBeforeColumns(),productInfoRo);
        }
        if (SyncTypeEnum.NEW.getValue().equals(eventType)){
            initDlrProduct(rowsRo.getAfterColumns(),productInfoRo);
        }
        if (SyncTypeEnum.UPDATE.getValue().equals(eventType)){
            initDlrProduct(rowsRo.getAfterColumns(),productInfoRo);
        }

        return productInfoRo;
    }

    @Override
    public ProductInfoRo bulidHyrProduct(BinlogRo binlogRo) {
        BinlogRowsRo rowsRo = binlogRo.getRows();
        ProductInfoRo productInfoRo = new ProductInfoRo();
        String eventType = binlogRo.getEventType();
        productInfoRo.setSyncType(eventType);
        productInfoRo.setDatabaseName(binlogRo.getSchemaName());
        productInfoRo.setTableName(binlogRo.getTableName());
        if (SyncTypeEnum.DELETE.getValue().equals(eventType)){
            initHyrProduct(rowsRo.getBeforeColumns(),productInfoRo);
        }
        if (SyncTypeEnum.NEW.getValue().equals(eventType)){
            initHyrProduct(rowsRo.getAfterColumns(),productInfoRo);
        }
        if (SyncTypeEnum.UPDATE.getValue().equals(eventType)){
            initHyrProduct(rowsRo.getAfterColumns(),productInfoRo);
        }

        return productInfoRo;
    }

    @Override
    public ProductInfoRo bulidBDBProduct(BinlogRo binlogRo) {
        BinlogRowsRo rowsRo = binlogRo.getRows();
        ProductInfoRo productInfoRo = new ProductInfoRo();
        String eventType = binlogRo.getEventType();
        productInfoRo.setSyncType(eventType);
        productInfoRo.setDatabaseName(binlogRo.getSchemaName());
        productInfoRo.setTableName(binlogRo.getTableName());
        if (SyncTypeEnum.DELETE.getValue().equals(eventType)){
            initBDBProduct(rowsRo.getBeforeColumns(),productInfoRo);
        }
        if (SyncTypeEnum.NEW.getValue().equals(eventType)){
            initBDBProduct(rowsRo.getAfterColumns(),productInfoRo);
        }
        if (SyncTypeEnum.UPDATE.getValue().equals(eventType)){
            initBDBProduct(rowsRo.getAfterColumns(),productInfoRo);
        }
        return productInfoRo;
    }

    @Override
    public ProductInfoRo bulidJJProduct(BinlogRo binlogRo) {
        BinlogRowsRo rowsRo = binlogRo.getRows();
        ProductInfoRo productInfoRo = new ProductInfoRo();
        String eventType = binlogRo.getEventType();
        productInfoRo.setSyncType(eventType);
        productInfoRo.setDatabaseName(binlogRo.getSchemaName());
        productInfoRo.setTableName(binlogRo.getTableName());
        if (SyncTypeEnum.DELETE.getValue().equals(eventType)){
            initJJProduct(rowsRo.getBeforeColumns(),productInfoRo);
        }
        if (SyncTypeEnum.NEW.getValue().equals(eventType)){
            initJJProduct(rowsRo.getAfterColumns(),productInfoRo);
        }
        if (SyncTypeEnum.UPDATE.getValue().equals(eventType)){
            initJJProduct(rowsRo.getAfterColumns(),productInfoRo);
        }
        productInfoRo.setSubSystemSign("LFJJ");
        syncDataService.syncProduct(productInfoRo);
        productInfoRo.setSubSystemSign("CZJJ");
        syncDataService.syncProduct(productInfoRo);
        productInfoRo.setSubSystemSign("KYZC");
        syncDataService.syncProduct(productInfoRo);
        productInfoRo.setSubSystemSign("FUND");
        syncDataService.syncProduct(productInfoRo);
        return  null;
    }

    @Override
    public ProductInfoRo bulidHxbxProduct(BinlogRo binlogRo) {
        BinlogRowsRo rowsRo = binlogRo.getRows();
        ProductInfoRo productInfoRo = new ProductInfoRo();
        String eventType = binlogRo.getEventType();
        productInfoRo.setSyncType(eventType);
        productInfoRo.setDatabaseName(binlogRo.getSchemaName());
        productInfoRo.setTableName(binlogRo.getTableName());
        if (SyncTypeEnum.DELETE.getValue().equals(eventType)){
            initHXBXProduct(rowsRo.getBeforeColumns(),productInfoRo);
        }
        if (SyncTypeEnum.NEW.getValue().equals(eventType)){
            initHXBXProduct(rowsRo.getAfterColumns(),productInfoRo);
        }
        if (SyncTypeEnum.UPDATE.getValue().equals(eventType)){
            initHXBXProduct(rowsRo.getAfterColumns(),productInfoRo);
        }

        return productInfoRo;
    }

    @Override
    public ProductInfoRo bulidChdProduct(BinlogRo binlogRo) {
        BinlogRowsRo rowsRo = binlogRo.getRows();
        ProductInfoRo productInfoRo = new ProductInfoRo();
        String eventType = binlogRo.getEventType();
        productInfoRo.setSyncType(eventType);
        productInfoRo.setDatabaseName(binlogRo.getSchemaName());
        productInfoRo.setTableName(binlogRo.getTableName());
        if (SyncTypeEnum.DELETE.getValue().equals(eventType)){
            initCHDProduct(rowsRo.getBeforeColumns(),productInfoRo);
        }
        if (SyncTypeEnum.NEW.getValue().equals(eventType)){
            initCHDProduct(rowsRo.getAfterColumns(),productInfoRo);
        }
        if (SyncTypeEnum.UPDATE.getValue().equals(eventType)){
            initCHDProduct(rowsRo.getAfterColumns(),productInfoRo);
        }

        return productInfoRo;
    }

    @Override
    public ProductInfoRo bulidJwbxProduct(BinlogRo binlogRo) {
        BinlogRowsRo rowsRo = binlogRo.getRows();
        ProductInfoRo productInfoRo = new ProductInfoRo();
        String eventType = binlogRo.getEventType();
        productInfoRo.setSyncType(eventType);
        productInfoRo.setDatabaseName(binlogRo.getSchemaName());
        productInfoRo.setTableName(binlogRo.getTableName());
        if (SyncTypeEnum.DELETE.getValue().equals(eventType)){
            initJWBXProduct(rowsRo.getBeforeColumns(),productInfoRo);
        }
        if (SyncTypeEnum.NEW.getValue().equals(eventType)){
            initJWBXProduct(rowsRo.getAfterColumns(),productInfoRo);
        }
        if (SyncTypeEnum.UPDATE.getValue().equals(eventType)){
            initJWBXProduct(rowsRo.getAfterColumns(),productInfoRo);
        }

        return productInfoRo;
    }

    @Override
    public ProductInfoRo bulidhhrcjdProduct(BinlogRo binlogRo) {
        BinlogRowsRo rowsRo = binlogRo.getRows();
        ProductInfoRo productInfoRo = new ProductInfoRo();
        String eventType = binlogRo.getEventType();
        productInfoRo.setSyncType(eventType);
        productInfoRo.setDatabaseName(binlogRo.getSchemaName());
        productInfoRo.setTableName(binlogRo.getTableName());
        if (SyncTypeEnum.DELETE.getValue().equals(eventType)){
            inithhrcjProduct(rowsRo.getBeforeColumns(),productInfoRo);
        }
        if (SyncTypeEnum.NEW.getValue().equals(eventType)){
            inithhrcjProduct(rowsRo.getAfterColumns(),productInfoRo);
        }
        if (SyncTypeEnum.UPDATE.getValue().equals(eventType)){
            inithhrcjProduct(rowsRo.getAfterColumns(),productInfoRo);
        }

        return productInfoRo;
    }

    private void inithhrcjProduct(List<BinlogColumnRo> beforeColumns, ProductInfoRo productInfoRo) {
        productInfoRo.setProductChannel("HHR_CF");
        productInfoRo.setSubSystemSign("HHR_CF");
        productInfoRo.setSystemSign("HHR");
        productInfoRo.setProductType(1+"");
        for (BinlogColumnRo b :beforeColumns) {
            String name = b.getName();
            Object value = b.getValue();
            try {
                if ("p_id".equals(name)) {
                    productInfoRo.setProductNo((String) value);
                }
                if ("p_id".equals(name)) {
                    productInfoRo.setTableKey((String) value);
                }
                if ("p_name".equals(name)) {
                    productInfoRo.setProductName((String) value);
                }
                if ("p_add_time".equals(name)) {

                    productInfoRo.setProductCreateTime(CustomerUtils.dateStr2Timestamp(name));

                }
            } catch (Exception e) {
                e.printStackTrace();
                log.error("------------------------" + name + "数据类型转换失败--------------------");
            }
        }
    }

    private void initJWBXProduct(List<BinlogColumnRo> afterColumns, ProductInfoRo productInfoRo) {
        productInfoRo.setProductChannel("JWBX");
        productInfoRo.setSubSystemSign("JWBX");
        productInfoRo.setSystemSign("BX");
        productInfoRo.setProductType(3+"");
        for (BinlogColumnRo b :afterColumns) {
            String name = b.getName();
            Object value = b.getValue();
            try {
                if ("id".equals(name)) {
                    productInfoRo.setProductNo((String) value);
                }
                if ("id".equals(name)) {
                    productInfoRo.setTableKey((String) value);
                }
                if ("name".equals(name)) {
                    productInfoRo.setProductName((String) value);
                }
                if ("create_time".equals(name)) {
                    if (StringUtils.isNotBlank((String) value)) {
                        productInfoRo.setProductCreateTime(CustomerUtils.dateStr2Timestamp((String) value));
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                log.error("------------------------" + name + "数据类型转换失败--------------------");
            }
        }
    }

    private void initCHDProduct(List<BinlogColumnRo> beforeColumns, ProductInfoRo productInfoRo) {
        productInfoRo.setProductChannel("CHD");
        productInfoRo.setSubSystemSign("CHD");
        productInfoRo.setSystemSign("CHD");
        productInfoRo.setProductType(6+"");
        for (BinlogColumnRo b :beforeColumns) {
            String name = b.getName();
            Object value = b.getValue();
            try {
                if ("prod_code".equals(name)) {
                    productInfoRo.setProductNo((String) value);
                }
                if ("id".equals(name)) {
                    productInfoRo.setTableKey((String) value);
                }
                if ("full_name".equals(name)) {
                    productInfoRo.setProductName((String) value);
                }
                if ("create_at".equals(name)) {
                    if (Long.valueOf((String) value)>0) {
                        productInfoRo.setProductCreateTime(new Timestamp(Long.valueOf((String) value)));
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                log.error("------------------------" + name + "数据类型转换失败--------------------");
            }
        }
    }

    private void initHXBXProduct(List<BinlogColumnRo> beforeColumns, ProductInfoRo productInfoRo) {
        productInfoRo.setProductChannel("BXHX");
        productInfoRo.setSubSystemSign("BXHX");
        productInfoRo.setSystemSign("BX");
        productInfoRo.setProductType(3+"");
        for (BinlogColumnRo b :beforeColumns) {
            String name = b.getName();
            Object value = b.getValue();
            try {
                if ("id".equals(name)) {
                    productInfoRo.setProductNo((String) value);
                }
                if ("id".equals(name)) {
                    productInfoRo.setTableKey((String) value);
                }
                if ("pro_name".equals(name)) {
                    productInfoRo.setProductName((String) value);
                }
                if ("created_at".equals(name)) {
                    productInfoRo.setProductCreateTime(new Timestamp(Long.valueOf((String) value)*1000));
                }
            } catch (Exception e) {
                e.printStackTrace();
                log.error("------------------------" + name + "数据类型转换失败--------------------");
            }
        }
    }

    private void initJJProduct(List<BinlogColumnRo> beforeColumns, ProductInfoRo productInfoRo) {
        productInfoRo.setProductChannel(CustomerConstants.FUND);
        productInfoRo.setSubSystemSign(CustomerConstants.FUND);
        productInfoRo.setSystemSign(CustomerConstants.FUND);
        productInfoRo.setProductType(Integer.toString(ProductTypeConstants.FUND));
        for (BinlogColumnRo b :beforeColumns) {
            String name = b.getName();
            Object value = b.getValue();
            try {
                if ("id".equals(name)) {
                    productInfoRo.setProductNo((String) value);
                }
                if ("id".equals(name)) {
                    productInfoRo.setTableKey((String) value);
                }
                if ("name".equals(name)) {
                    productInfoRo.setProductName((String) value);
                }
                if ("register_date".equals(name)) {
                    productInfoRo.setProductCreateTime(CustomerUtils.dateStr2Timestamp((String) value));
                }
            } catch (Exception e) {
                e.printStackTrace();
                log.error("------------------------" + name + "数据类型转换失败--------------------");
            }
        }
    }

    private void initBDBProduct(List<BinlogColumnRo> afterColumns, ProductInfoRo productInfoRo) {
        productInfoRo.setProductChannel(CustomerConstants.BDB);
        productInfoRo.setSubSystemSign(CustomerConstants.BDB);
        productInfoRo.setSystemSign(CustomerConstants.BX);
        productInfoRo.setProductType(Integer.toString(ProductTypeConstants.INSURANCE));
        for (BinlogColumnRo b :afterColumns) {
            String name = b.getName();
            Object value = b.getValue();
            try {
                if ("productcode".equals(name)){
                    productInfoRo.setProductNo((String) value);
                }
                if ("id".equals(name)){
                    productInfoRo.setTableKey((String) value);
                }
                if ("productname".equals(name)){
                    productInfoRo.setProductName((String) value);
                }
                if ("create_time".equals(name)){
                    productInfoRo.setProductCreateTime( CustomerUtils.dateStr2Timestamp((String) value));
                }
            } catch (Exception e) {
                e.printStackTrace();
                log.error("------------------------"+name+"数据类型转换失败--------------------");
            }
        }

    }

    private void initHyrProduct(List<BinlogColumnRo> binlogColumnRos, ProductInfoRo productInfoRo) {
        productInfoRo.setProductChannel(CustomerConstants.HYR);
        productInfoRo.setSubSystemSign(CustomerConstants.HYR);
        productInfoRo.setSystemSign(CustomerConstants.HYR);
        productInfoRo.setProductType(Integer.toString(ProductTypeConstants.P2P_LEND));
        for (BinlogColumnRo b :binlogColumnRos) {
            String name = b.getName();
            Object value = b.getValue();
            try {
                if (TableNameConstants.HYR_PRODUCT_NO.equals(name)){
                    productInfoRo.setProductNo((String) value);
                }
                if (TableNameConstants.HYR_PRODUCT_TABLE_KEY.equals(name)){
                    productInfoRo.setTableKey((String) value);
                }
                if (TableNameConstants.HYR_PRODUCT_NAME.equals(name)){
                    productInfoRo.setProductName((String) value);
                }
                if (TableNameConstants.HYR_PRODUCT_COLUMN_CREATE_TIME.equals(name)){
                    productInfoRo.setProductCreateTime( CustomerUtils.dateStr2Timestamp((String) value));
                }

            } catch (Exception e) {
                e.printStackTrace();
                log.error("------------------------"+name+"数据类型转换失败--------------------");

            }

        }
    }

    private void initDlrProduct(List <BinlogColumnRo> binlogColumnRos , ProductInfoRo productInfoRo){
        productInfoRo.setProductChannel(CustomerConstants.DLR);
        productInfoRo.setSubSystemSign(CustomerConstants.DLR);
        productInfoRo.setSystemSign(CustomerConstants.DLR);
        productInfoRo.setProductType(Integer.toString(ProductTypeConstants.P2P_LEND));
        for (BinlogColumnRo b :binlogColumnRos) {
            String name = b.getName();
            Object value = b.getValue();
            try {
                if (TableNameConstants.DLR_PRODUCT_COLUMN_ID.equals(name)){
                    productInfoRo.setProductNo((String) value);
                }
                if (TableNameConstants.DLR_PRODUCT_COLUMN_INCREMENT_ID.equals(name)){
                    productInfoRo.setTableKey((String) value);
                }
                if (TableNameConstants.DLR_PRODUCT_COLUMN_PRODUCT_NAME.equals(name)){
                    productInfoRo.setProductName((String) value);
                }
                if (TableNameConstants.DLR_PRODUCT_COLUMN_CREATE_TIME.equals(name)){
                    productInfoRo.setProductCreateTime( CustomerUtils.dateStr2Timestamp((String) value));
                }
                if (TableNameConstants.DLR_PRODUCT_COLUMN_UPDATE_TIME.equals(name)){
                    productInfoRo.setProductUpdateTime(CustomerUtils.dateStr2Timestamp((String) value));
                }

            } catch (Exception e) {
                e.printStackTrace();
                log.error("------------------------"+name+"数据类型转换失败--------------------");

            }

        }


    }






}
